 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.activity
 * File     : Contacts.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : Nov 20, 2011
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.activity;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.bean.Contact;
import net.solosky.easyavatar.utils.Globals;
import net.solosky.easyavatar.utils.Views;
import net.solosky.easyavatar.view.ContactListAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 *
 * 选择联系人
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class Contacts extends Base implements OnClickListener, OnItemClickListener {
	private ListView mContactListView;
	private ContactListAdapter mContactListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.ui_contacts);
		setTitle(R.string.select_contact);
		setupUI();
		
	}
	
	
	
	private void setupUI(){
		mContactListView = (ListView) findViewById(R.id.lv_contacts);
		mContactListAdapter = new ContactListAdapter(this, Globals.CONTACT_MANAGER.getContactList());
		mContactListView.setAdapter(mContactListAdapter);
		mContactListView.setOnItemClickListener(this);
		Views.batchSetClickListener(new int[]{R.id.bt_ok, R.id.bt_cancel}, this, this);
		setupBackButton();
		setRightButton(R.string.ok, this);
		
		setResult(RESULT_CANCELED);
		
	}

	@Override
	public void onClick(View view) {
		switch(view.getId()){
		case R.id.bt_ok:
		case R.id.bt_title_right:
			int selected = mContactListAdapter.getSelectedPosition();
			if(selected!=-1){
				Contact contact = (Contact) mContactListAdapter.getItem(selected);
				Intent intent = new Intent();
				intent.putExtra("friend.fid", getIntent().getStringExtra("friend.fid"));
				intent.putExtra("contact.cid", contact.getId());
				setResult(RESULT_OK, intent);
				finish();
			}else{
				Views.showToast(R.string.err_contact_not_selected);
			}
			break;
			
		case R.id.bt_cancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		
		}
	}



	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		mContactListAdapter.setSeletedPoistion(position);
		mContactListAdapter.notifyDataSetChanged();
	}

	
}
